package edu.sctu.zhuyingzhi.month201506.day20150623.jsonTest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.*;

/**
 * Created by user13 on 2015/6/23.
 */
public class JsonUtil1 {
    static HttpClientBuilder builder = HttpClientBuilder.create();
    static CloseableHttpClient client = builder.build();

    /**
     * get方法
     * @throws java.io.IOException
     */
    public static String GetJson(String path) throws IOException {
        HttpGet get = new HttpGet(path);
        HttpResponse response = client.execute(get);
        HttpEntity entity =response.getEntity();

        InputStream inputStream = entity.getContent();

        String str;
        str = steamToString(inputStream);


        get.releaseConnection();
        return str;
    }

    /**
     * json数据解析
     * @param json
     * @return
     */
    public static MainData jsonAnalytical(String json) throws IOException {
        JSONObject jsonObject = JSONObject.parseObject(json);
        MainData mainData = new MainData();

        mainData.error = jsonObject.getInteger("error");
        mainData.count = jsonObject.getString("count");
        mainData.message = jsonObject.getString("message");
        mainData.page = jsonObject.getString("page");
        mainData.size = jsonObject.getString("size");
        JSONArray list = jsonObject.getJSONArray("list");

        for (int i = 0;i<list.size();i++){
            Goods goods = new Goods();
            String x = list.getString(i);
            JSONObject jsonObject1 = JSONObject.parseObject(x);

            goods.goods_id = jsonObject1.getString("goods_id");
            goods.goods_name= jsonObject1.getString("goods_name");
            goods.goods_price = jsonObject1.getString("goods_price");

            mainData.list.add(goods);
        }
        serializableGood(mainData);
        return mainData;
    }

    /**
     * 快速JSON解析
     * @param
     * @throws IOException
     */
    public static MainData fastJsonAnalytical(String json){
        MainData mainData ;

        mainData = JSONObject.parseObject(json,MainData.class);

        return mainData;
    }
    public static void serializableGood(MainData mainData) throws IOException {
        ObjectOutputStream goods = new ObjectOutputStream(new FileOutputStream(new File("D:/goods.txt")));
        goods.writeObject(mainData);
        goods.close();
    }
    /**
     * 流变为字符串
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String steamToString(InputStream inputStream) throws IOException {
        byte[] buffer = new  byte[1024];
        int count;
        String str = "     ";

        while((count = inputStream.read(buffer)) !=-1){                  //为什么必须循环4次才能读完
            str = str + new String(buffer,0,count,"utf-8");
        }

        return str;
    }
}
